package test0109;

import test1231.EE;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test05 {
    public static void main(String[] args) {
        // (5)根据需求写出实现代码

        // 创建公司
        CC c = new CC("IBM");
        // 创建员工
        EE e1 = new EE("张三", 30, 3000);
        EE e2 = new EE("李四", 40, 4000);
        EE e3 = new EE("王五", 50, 5000);
        // 公司添加员工(集合)
        c.add(e1);
        c.add(e2);
        c.add(e3);
        c.add(new EE("赵六", 60, 6000));
        // 打印员工信息
        c.printAll();
        // 找出年龄最大员工
        EE x = c.m1();
        // 找出工资最低工资
        EE y = c.m2();
        // 找出李四员工
        EE z = c.m3("李四");
        // 求平均工资
        double d = c.m4();
        // 找工资高于平均工资的员工
        List list = c.m5();
        System.out.println("工资高于平均工资的员工: " + list);

    }

    public static class CC {

            String name;
            List<EE> employees = new ArrayList<>();

            public CC(String name) {
                this.name = name;
            }

            public void add(EE employee) {
                employees.add(employee);
            }

            public void printAll() {
                for (EE employee : employees) {
                    System.out.println(employee);
                }
            }

            public EE m1() { // 找出年龄最大员工
                EE oldest = Collections.max(employees, Comparator.comparingInt(ee -> ee.getAge()));
                return oldest;
            }

            public EE m2() { // 找出工资最低员工
                EE lowestSalary = Collections.min(employees, Comparator.comparingDouble(ee -> ee.getSalary()));
                return lowestSalary;
            }

            public EE m3(String name) { // 找出指定姓名员工
                for (EE employee : employees) {
                    if (employee.getName().equals(name)) {
                        return employee;
                    }
                }
                return null; // 如果没有找到，返回null
            }

            public double m4() { // 求平均工资
                double totalSalary = 0;
                for (EE employee : employees) {
                    totalSalary += employee.getSalary();
                }
                return totalSalary / employees.size();
            }

            public List<EE> m5() { // 找工资高于平均工资的员工
                double avgSalary = m4();
                List<EE> aboveAvgSalary = new ArrayList<>();
                for (EE employee : employees) {
                    if (employee.getSalary() > avgSalary) {
                        aboveAvgSalary.add(employee);
                    }
                }
                return aboveAvgSalary;
            }
        }
}

